package com.sofwin.configer;

import com.sofwin.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @packageName: com.sofwin.configer
 * @user: andyliu
 * @date: 2021/12/13 15:11
 * @email 115176513@qq.com
 * @description: TODO
 */
@Configuration
public class MvcConfiger implements WebMvcConfigurer {

    @Bean
    public LoginInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }

    @Bean("multipartResolver")
    public CommonsMultipartResolver getMultipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxInMemorySize(100000);
        resolver.setMaxUploadSizePerFile(100000);
        resolver.setDefaultEncoding("utf-8");
        return resolver;
    }

    /**
     * <mvc:interceptors></mvc:interceptors>
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.getLoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").excludePathPatterns("/verification")
        .excludePathPatterns("/static/**");
    }

    /**
     * 配置静态资源
     * webapp 下static目录存放静态资源
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("/static/");

    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * 配置视图解析器
     * @param registry
     */
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/",".jsp");
    }
}
